package com.sc.study.student.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.sc.common.utils.DateUtils;
import com.sc.study.clazz.domain.ScClazz;
import com.sc.study.clazz.domain.ScClazzSubject;
import com.sc.study.clazz.mapper.ScClazzMapper;
import com.sc.study.clazz.mapper.ScClazzSubjectMapper;
import com.sc.study.exam.domain.ScStudentTestexam;
import com.sc.study.exam.domain.ScStudentTestexamResult;
import com.sc.study.exam.domain.ScTestexam;
import com.sc.study.exam.mapper.ScStudentTestexamMapper;
import com.sc.study.exam.mapper.ScStudentTestexamResultMapper;
import com.sc.study.exam.mapper.ScTestexamMapper;
import com.sc.study.exam.mapper.ScTestexamTypeMapper;
import com.sc.study.foundation.mapper.ScGradeMapper;
import com.sc.study.foundation.mapper.ScSubjectMapper;
import com.sc.study.student.domain.ScStudent;
import com.sc.study.student.domain.ScStudentSubject;
import com.sc.study.student.mapper.ScStudentMapper;
import com.sc.study.student.mapper.ScStudentSubjectMapper;
import com.sc.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.student.mapper.ScStudentClazzMapper;
import com.sc.study.student.domain.ScStudentClazz;
import com.sc.study.student.service.IScStudentClazzService;

/**
 * 学员班级Service业务层处理
 *
 * @author sc
 * @date 2022-02-27
 */
@Service
public class ScStudentClazzServiceImpl implements IScStudentClazzService {
    @Autowired
    private ScStudentClazzMapper scStudentClazzMapper;
    @Autowired
    private ScClazzMapper scClazzMapper;
    @Autowired
    private ScStudentMapper scStudentMapper;
    @Autowired
    private ScSubjectMapper scSubjectMapper;
    @Autowired
    private ScGradeMapper scGradeMapper;
    @Autowired
    private ScStudentSubjectMapper scStudentSubjectMapper;
    @Autowired
    private ScClazzSubjectMapper scClazzSubjectMapper;
    @Autowired
    private ScStudentTestexamMapper scStudentTestexamMapper;
    @Autowired
    private ScTestexamMapper scTestexamMapper;
    @Autowired
    private ScTestexamTypeMapper scTestexamTypeMapper;
    @Autowired
    private ISysConfigService sysConfigService;



    /**
     * 学员班级查看
     * 查询学员班级包含班级、学员、年级 并且查看这个学员的历史班级
     *
     * @param id 学员班级主键
     * @return 学员班级
     */
    @Override
    public ScStudentClazz selectScStudentClazzIncludeHistoryClazz(Long id) {
        ScStudentClazz scStudentClazz = scStudentClazzMapper.selectScStudentClazzById(id);
        int studentCode = Integer.parseInt(sysConfigService.selectConfigByKey("student_code"));
        // 如果编号是学员
        if(scStudentClazz.getClazzpersontypeid()==studentCode){
            // 班级
            scStudentClazz.setScClazz(scClazzMapper.selectScClazzById(scStudentClazz.getClazzid()));
            // 学员
            scStudentClazz.setScStudent(scStudentMapper.selectScStudentById(scStudentClazz.getStudentid()));
            // 年级
            scStudentClazz.setScGrade(scGradeMapper.selectScGradeById(scStudentClazz.getGradeid()));


            ScStudentClazz studentClazz=new ScStudentClazz();
            studentClazz.setStudentid(scStudentClazz.getStudentid());

            // 查询学员班级表里改学员离开过的记录
            List<ScStudentClazz> scStudentClazzList = scStudentClazzMapper.selectScStudentClazzOutTimeIsNotNull(studentClazz);
            // 该学员待过的历史班级
            List<ScClazz> scClazzList=scStudentClazzList.stream().map(item->scClazzMapper.selectScClazzById(item.getClazzid())).collect(Collectors.toList());

            // 查询该学员参加过哪些考试
            ScStudentTestexam scStudentTestExam=new ScStudentTestexam();
            scStudentTestExam.setStudentid(scStudentClazz.getStudentid());

            // 查出学员考试
            List<ScStudentTestexam> scStudentTestExamList = scStudentTestexamMapper.selectScStudentTestexamList(scStudentTestExam);
            // 根据学员考试查出考试信息
            scStudentTestExamList.forEach(item->{
                ScTestexam scTestexam = scTestexamMapper.selectScTestexamById(item.getTestexamid());
                scTestexam.setScTestexamType(scTestexamTypeMapper.selectScTestexamTypeById(scTestexam.getTestexamtypeid()));

                item.setTestexam(scTestexam);
            });

            scStudentClazz.setScStudentTestExamList(scStudentTestExamList);

            scStudentClazz.setScOldClazzList(scClazzList);
        }

        return scStudentClazz;
    }


    /**
     * 查询学员班级
     *
     * @param id 学员班级主键
     * @return 学员班级
     */
    @Override
    public ScStudentClazz selectScStudentClazzById(Long id) {
        return scStudentClazzMapper.selectScStudentClazzById(id);
    }

    /**
     * 查询学员班级列表包含班级，学员，年级
     *
     * @param scStudentClazz 学员班级
     * @return 学员班级
     */
    @Override
    public List<ScStudentClazz> selectScStudentClazzList(ScStudentClazz scStudentClazz) {
        int studentCode = Integer.parseInt(sysConfigService.selectConfigByKey("student_code"));
        scStudentClazz.setClazzpersontypeid(studentCode);
        return scStudentClazzMapper.selectScStudentClazzListIncludeClazzAndGradeAndStudent(scStudentClazz);
    }

    /**
     * 新增学员班级
     *
     * @param scStudentList  学员集合
     * @param studentClazzId 班级Id
     * @return
     */
    @Override
    public int insertScStudentClazz(List<ScStudent> scStudentList, Long studentClazzId) {

        //studentClazzId : 前台传入的是班级id
        ScClazz scClazz = scClazzMapper.selectScClazzById(studentClazzId);
        int clazzPersonTypeId = Integer.parseInt(sysConfigService.selectConfigByKey("student_code"));

        scStudentList.forEach(item -> {

            ScStudentClazz scStudentClazz = new ScStudentClazz(studentClazzId, item.getId(), scClazz.getGradeid(),clazzPersonTypeId,DateUtils.getLocalDateTime());
            scStudentClazzMapper.insertScStudentClazz(scStudentClazz);
            //修改学员班级Id
            item.setStudentclazzid(scStudentClazz.getId());
            scStudentMapper.updateScStudent(item);

            ScClazzSubject scClazzSubject = new ScClazzSubject();
            scClazzSubject.setClazzid(studentClazzId);
            List<ScClazzSubject> clazzSubjectList = scClazzSubjectMapper.selectScClazzSubjectList(scClazzSubject);
            //循环添加学员科目信息
            clazzSubjectList.forEach(clazzSubject -> {
                clazzSubject.setScSubject(scSubjectMapper.selectScSubjectBySubid(clazzSubject.getSubid()));
                scStudentSubjectMapper.insertScStudentSubject(new ScStudentSubject(clazzSubject.getSubid(), item.getId(), clazzSubject.getScSubject().getSubtime(), 0L));
            });
        });

        return 1;
    }

    @Override
    public int insertScStudentClazz(ScStudentClazz scStudentClazz) {
        return scStudentClazzMapper.insertScStudentClazz(scStudentClazz);
    }


    /**
     * 修改学员班级
     *
     * @param scStudentClazz 学员班级
     * @return 结果
     */
    @Override
    public int updateScStudentClazz(ScStudentClazz scStudentClazz) {
        return scStudentClazzMapper.updateScStudentClazz(scStudentClazz);
    }

    /**
     * 批量删除学员班级
     *
     * @param ids 需要删除的学员班级主键
     * @return 结果
     */
    @Override
    public int deleteScStudentClazzByIds(Long[] ids) {

        Arrays.stream(ids).forEach(id->{
            ScStudent scStudent=new ScStudent();
            scStudent.setStudentclazzid(id);

            // 找到对应的学员
            List<ScStudent> scStudentList = scStudentMapper.selectScStudentList(scStudent);

            // 修改学员状态为空
            scStudentList.forEach(item->{
                scStudentMapper.updateScStudentStudentClazzId(item);
                ScStudentSubject scStudentSubject=new ScStudentSubject();
                scStudentSubject.setStudentid(item.getId());
                // 删除学员课程
                scStudentSubjectMapper.deleteScStudentSubject(scStudentSubject);
            });
        });

        // 删除学员班级
        return scStudentClazzMapper.deleteScStudentClazzByIds(ids);
    }

    /**
     * 删除学员班级信息
     *
     * @param id 学员班级主键
     * @return 结果
     */
    @Override
    public int deleteScStudentClazzById(Long id) {
        return scStudentClazzMapper.deleteScStudentClazzById(id);
    }
}
